package com.yly.utils;

import java.io.File;

public class IOUtil {
	
	/**
	 * <p>Title: 获得文件夹</p>
	 * <p>Description: 获得文件夹</p>
	 * @param folderPath 文件夹路径
	 * @return 文件夹对象
	 */
	public static File getFolder(String folderPath) {
		
		File dirFile = new File(folderPath);
		
		if (!dirFile.exists()) {
			boolean bFile = dirFile.mkdir();
			if (!bFile) {
				dirFile = null;
			}
		}
		return dirFile;
	}
	
	/**
	 * <p>Title: 获得文件</p>
	 * <p>Description: 获得文件</p>
	 * @param folderPath 文件路径
	 * @return 文件对象
	 */
	public static File getFolderAndFile(String folderPath) {
		File file = null;
		if(folderPath != null && !"".equals(folderPath)){
			folderPath = folderPath.replace("\\", "|*|");
			folderPath = folderPath.replace("/", "|*|");
			String [] folderPaths = folderPath.split("\\|\\*\\|");
			String path = folderPaths[0];
			for(int i= 1; i < folderPaths.length; i++){
				path = path + File.separator + folderPaths[i];
				if(i != folderPaths.length -1){
					file = getFolder(path);
				}else{
					file = getFile(path);
				}
				
			}
		}
		return file;
	}
	
	/**
	 * <p>Title: 获得文件</p>
	 * <p>Description: 获得文件</p>
	 * @param folderPath 文件路径
	 * @return 文件对象
	 */
	public static File getFile(String folderPath) {
		File file = new File(folderPath);
		return file;
	}
	
	
	public static String getResourcePath(String relativePath){
		String newPath = "";
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		path = path.replace("\\", "/");
		int num = relativePath.lastIndexOf("../") + 3;
		String tmpPath = "";
		String lastPath = "";
		if(num != -1){
			tmpPath = relativePath.substring(0, num);
			lastPath = relativePath.substring(num, relativePath.length());
			int n = tmpPath.length()/3;
			int lastTmp = path.lastIndexOf("/");
			if(lastTmp == path.length() -1){
				path = path.substring(0,lastTmp);
			}
			int fristTmp = path.indexOf("/");
			if(fristTmp == 0){
				path = path.substring(1);
			}
			for(int i = 0; i < n; i++){
				path = path.substring(0,path.lastIndexOf("/"));
			}
			newPath = path + File.separator +lastPath;
			if(!"/".equals(File.separator)){
				newPath = newPath.replace("/", "\\");
			}
		}
		return newPath;
	}
	
	

	
	public static void main(String[] args) {
		//getResourcePath("../../cjtest/ddee/ffrr");
	
	}
}
